{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# hvPlot.bar\n",
    "\n",
    "```{eval-rst}\n",
    ".. currentmodule:: hvplot\n",
    "\n",
    ".. automethod:: hvPlot.bar\n",
    "```\n",
    "\n",
    "## Backend-specific styling options\n",
    "\n",
    "```{eval-rst}\n",
    ".. backend-styling-options:: bar\n",
    "```\n",
    "\n",
    "## Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simple vertical bar plot\n",
    "\n",
    "Bar plots are ideal for comparing quantities across different categories. Here’s how to create a basic vertical bar chart."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.DataFrame({\n",
    "    'Project' : ['hvPlot', 'HoloViews', 'Panel'],\n",
    "    'Downloads': [519000, 852000, 1371000],\n",
    "})\n",
    "\n",
    "df.hvplot.bar(x='Project', y='Downloads', title='Project downloads', width=500)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Multi-index bar plot\n",
    "\n",
    "When working with multi-index data, hvPlot can automatically interpret the hierarchical index to create nested categories on the x-axis, the outer index level being displayed as the outer category."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import pandas as pd\n",
    "\n",
    "multi_index_df = pd.DataFrame({\n",
    "    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],\n",
    "    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],\n",
    "    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],\n",
    "}).set_index(['Project', 'Source'])\n",
    "print(multi_index_df.head(3))\n",
    "\n",
    "multi_index_df.hvplot.bar(title='Project downloads (multi-index)', yformatter='%i')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can instead stack on the y-axis the values of the nested index (`'project'` in this example), by setting `stacked` to `True`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import pandas as pd\n",
    "\n",
    "multi_index_df = pd.DataFrame({\n",
    "    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],\n",
    "    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],\n",
    "    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],\n",
    "}).set_index(['Project', 'Source'])\n",
    "print(multi_index_df.head(3))\n",
    "\n",
    "multi_index_df.hvplot.bar(title='Project downloads (stacked multi-index)', stacked=True, yformatter='%i')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Wide-form data\n",
    "\n",
    "In wide-form data, each variable is in its own column. To plot multiple categories on the x-axis when the data is in this form, you need to provide a list of columns to `y`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import pandas as pd\n",
    "\n",
    "wide_df = pd.DataFrame({\n",
    "    'Project' : ['hvPlot', 'HoloViews', 'Panel'],\n",
    "    'Conda' : [112000, 132000, 171000],\n",
    "    'PyPI': [407000, 720000, 1200000],\n",
    "})\n",
    "\n",
    "wide_df.hvplot.bar(\n",
    "    x='Project',\n",
    "    y=['Conda', 'PyPI'],\n",
    "    title='Project downloads (wide-form)',\n",
    "    group_label='Source',\n",
    "    rot=45,\n",
    "    width=500,\n",
    "    yformatter='%i',\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting `stacked=True` produces a stacked plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import pandas as pd\n",
    "\n",
    "wide_df = pd.DataFrame({\n",
    "    'Project' : ['hvPlot', 'HoloViews', 'Panel'],\n",
    "    'Conda' : [112000, 132000, 171000],\n",
    "    'PyPI': [407000, 720000, 1200000],\n",
    "})\n",
    "\n",
    "wide_df.hvplot.bar(\n",
    "    x='Project',\n",
    "    y=['Conda', 'PyPI'],\n",
    "    stacked=True,\n",
    "    title='Project downloads (wide-form stacked)',\n",
    "    rot=45,\n",
    "    width=500,\n",
    "    yformatter='%i',\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Long-form data\n",
    "\n",
    "In long-form data, each observation is in its own row. Setting `by` can be used to break down the x-axis in multiple sub-categories."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import pandas as pd\n",
    "\n",
    "long_df = pd.DataFrame({\n",
    "    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],\n",
    "    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],\n",
    "    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],\n",
    "})\n",
    "\n",
    "long_df.hvplot.bar(\n",
    "    x='Project',\n",
    "    y='Downloads',\n",
    "    by='Source',\n",
    "    title='Project downloads (long-lorm)',\n",
    "    rot=45,\n",
    "    width=500,\n",
    "    yformatter='%i',\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting `stacked=True` produces a stacked plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import pandas as pd\n",
    "\n",
    "long_df = pd.DataFrame({\n",
    "    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],\n",
    "    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],\n",
    "    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],\n",
    "})\n",
    "\n",
    "long_df.hvplot.bar(\n",
    "    x='Project',\n",
    "    y='Downloads',\n",
    "    by='Source',\n",
    "    stacked=True,\n",
    "    title='Project downloads (long-lorm stacked)',\n",
    "    rot=45,\n",
    "    width=500,\n",
    "    yformatter='%i',\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Timeseries on the x-axis\n",
    "\n",
    "The x-axis can be a timeseries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "\n",
    "df = hvplot.sampledata.stocks(\"pandas\").set_index(\"date\").resample(\"1ME\").mean() - 1\n",
    "\n",
    "df.hvplot.bar(y=\"Netflix\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Xarray example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.xarray  # noqa\n",
    "\n",
    "ds = hvplot.sampledata.air_temperature(\"xarray\")\n",
    "ds = ds.sel(lon=285.,lat=40.).groupby('time.dayofyear').mean() - 273.15\n",
    "\n",
    "ds.hvplot.bar(y=\"air\", title=\"Air temperature by day\", ylabel=\"T [°C]\", c=\"air\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Customizing bar colors\n",
    "\n",
    "You can customize the appearance of bar plots by assigning specific colors to each variable. This makes it easier to distinguish between multiple bars in stacked or grouped charts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.DataFrame({\n",
    "    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],\n",
    "    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],\n",
    "    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],\n",
    "})\n",
    "\n",
    "df.hvplot.bar(\n",
    "    x='Project',\n",
    "    y='Downloads',\n",
    "    by='Source',\n",
    "    color=['forestgreen', 'orange'],\n",
    "    title='Custom bar colors',\n",
    "    stacked=True,\n",
    "    legend='top_left',\n",
    "    width=500,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
